/*
cd /projects/data_commons/cw_code/
qstata cw_geo_msa_czone.do &

"Hybrid" MSA and CZONE.
*/

di "Started at $S_DATE $S_TIME"

global dir_datac = "/projects/data_commons/cw/"

tempfile ds_temp
global ds_temp = "`ds_temp'"

//==============================================================================
// Assign czone to rural MSA

capture program drop f_msa_czone
program f_msa_czone

gen msa_czone = msa

/* REDACTED:
An update is made to the msa_czone variable 
*/

tab msa_str if msa_czone == .

keep fips msa_czone
drop if msa_czone == .
sort fips
recast long msa
compress

end

//------------------------------------------------------------------------------
// MSA2010

use ${dir_datac}/cw_geo_fips_msa, clear
merge 1:1 fips using ${dir_datac}/cw_geo_fips_czone

f_msa_czone

rename msa_czone msa2010cz

save ${dir_datac}/cw_geo_fips_msa_czone, replace

//------------------------------------------------------------------------------
// MSA1983

use ${dir_datac}/cw_geo_fips_msa1983, clear
merge 1:1 fips using ${dir_datac}/cw_geo_fips_czone
rename msa1983 msa
rename msa1983_str msa_str

f_msa_czone

rename msa_czone msa1983cz

merge 1:1 fips using ${dir_datac}/cw_geo_fips_msa_czone
list if _merge == 1
drop _merge

sort fips
save ${dir_datac}/cw_geo_fips_msa_czone, replace

//------------------------------------------------------------------------------
// Additional saving
export delimited ${dir_datac}/cw_geo_fips_msa_czone.csv, replace
saveold ${dir_datac}/cw_geo_fips_msa_czone_v12, replace v(12)

di "Ended at $S_DATE $S_TIME"
// End of do file
